﻿using Microsoft.EntityFrameworkCore;
using ZeroSum.Application.Datas;
using ZeroSum.Application.Extensions;
using ZeroSum.Domain.Repositories;
using ZeroSum.Infrastructure.Extensions;
using ZeroSum.Mapper.Mappers;
using ZeroSumDBM.Application.Services.Icons.Dto.In;
using ZeroSumDBM.Application.Services.Icons.Dto.Query;
using ZeroSumDBM.Core.Aggregates;

namespace ZeroSumDBM.Application.Services.Icons;

public class IconService : IIconService
{
    private readonly IRepository<Icon> _repository;

    public IconService(IRepository<Icon> repository)
    {
        _repository = repository;
    }

    public async Task<PagedList<PageIconDto>> Page(PageIconQuery input)
    {
        var keywords = !string.IsNullOrEmpty(input.Keyword?.Trim());
        var icons = await _repository.Query
#pragma warning disable CS8602 // 解引用可能出现空引用。
            .WhereIf(keywords, icon => EF.Functions.Like(icon.Name, $"%{input.Keyword.Trim()}%"))
#pragma warning restore CS8602 // 解引用可能出现空引用。
            .Select(x => x.MapTo<PageIconDto>())
            .ToPagedListAsync(input.PageIndex, input.PageSize);
        return icons;
    }
}